package com.zx.mobile.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.MessageSendLogService;
import com.zx.map.service.SettleTypeService;
import com.zx.map.service.model.MerchantInfoDetailModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.service.proxy.MessageSendProxy;
import com.zx.mobile.utils.WebUtils;

@Controller
@RequestMapping("/settleType")
public class SettleTypeController {

	@Autowired
	private SettleTypeService settleTypeService;
	@Autowired
	private MessageSendLogService messageSendLogService;
	@Autowired
	private MerchantInfoService merchantInfoService;

	@RequestMapping("/list")
	@ResponseBody
	public Map list(HttpServletRequest request) {

		try {
			return CommonResponseUtils.successResponse(settleTypeService.list());
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/sendVerifyCode")
	@ResponseBody
	public Map send(HttpServletRequest request) {
		try {
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			MessageSendProxy.getMessageResponse(MobileCommonHelper.MESSAGETYPE_SETTLE_TYPE, merchant.getTelephone());
			return CommonResponseUtils.successResponse();
		} catch (Throwable e) {
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}
	
	
	@RequestMapping("/update")
	@ResponseBody
	public Map update(HttpServletRequest request) {

		try {
			String settleTypeId = request.getParameter("settleTypeId");
			String verifyCode = request.getParameter("verifyCode");
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			if(!messageSendLogService.checkVerifyCode(merchant.getTelephone(),verifyCode,MobileCommonHelper.MESSAGETYPE_SETTLE_TYPE)) {
				throw new Exception("验证码错误");
			}
			MerchantInfoModel mm =merchantInfoService.getModelByKey(merchant.getMerchantId());
			mm.setSettleRateType(Integer.parseInt(settleTypeId));
			merchantInfoService.update(mm);
			
			return CommonResponseUtils.successResponse();
		} catch (Throwable e) {
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

}
